<template>
  <div id="app">
    <router-view/>
  </div>
</template>
<script>
  import storage from '@/storage'
  export default {
    data() {
      return {
        res: {}
      }
    },
    mounted() {
      // 本地加载请求静态json文件的形式
      // this.axios.get('/mock/user/login.json').then((res) => {
      //   this.res = res;
      // })
      // 通过easy-mock平台实现数据mock
      // this.axios.get('/user/login.json').then((res) => {
      //   this.res = res;
      // })
      // 本地集成mock api cnpm i mockjs --save-dev
      // this.axios.get('/user/login').then((res) => {
      //   this.res = res;
      // })
      if(this.$cookie.get('userId')) {
        this.getUser();
        this.getCartCount();
      }
    },
    methods: {
      getUser () {
        this.axios({
          method:'get',
          url: '/user'
        }).then((res={}) => {
          // to-do保存到vuex中
          this.$store.dispatch('saveUserName', res.username)
        })
      },
      getCartCount () {
        this.axios({
          method:'get',
          url: '/carts/products/sum',
        }).then((res) => {
          // to-do保存到vuex中
           this.$store.dispatch('saveCartCount', res)
          // if (res.status == 0) {
          //   // this.$router.push()
          //   this.$store.dispatch('saveCartCount', res)
          // }
        })
      }
    },
}
</script>
<style lang="scss">
@import './assets/scss/reset.scss';
@import './assets/scss/config.scss';
@import './assets/scss/modal.scss';
@import './assets/scss/mixin.scss';
@import './assets/scss/button.scss';
</style>
